#include "ClientRegionManager.hpp"

ClientRegionManager::ClientRegionManager():RegionManager(){
    //Register client region.
    ClientRegion registerRegion;
    registerObject(&registerRegion);
    
    init("Objects/c_regions.lua");
}

void ClientRegionManager::update(unsigned int frameTime){
    for(unsigned short i = 0; i < size; i++){
		if(regions[i]){
			runUpdate(regions[i]);
		}
	}
}

Region* ClientRegionManager::createRegion(const char* name){
    for(unsigned short i = 0; i < size; i++){
        if(regions[i] == 0){
            regions[i] = new ClientRegion();
            ((ClientRegion*)regions[i])->index = i;

            runStartup((ClientRegion*)regions[i], name);
            
            return regions[i];
        }
    }
    return 0;
}

bool ClientRegionManager::destroyRegion(Region* region){
    for(unsigned short i = 0; i < size; i++){
        if(regions[i] == region){
			runDestroy(regions[i]);
            delete regions[i];
            regions[i] = 0;
            return true;
        }
    }
    return false;
}

void ClientRegionManager::bind(lua_State* luaState){

	luabind::module(luaState)[
        luabind::class_<ClientRegionManager>("ClientRegionManager")
			.def_readonly("size", &ClientRegionManager::size)
			.def("destroyRegion", &ClientRegionManager::destroyRegion)
    ];
}

void ClientRegionManager::push(lua_State* luaState){
	luabind::object o = luabind::object(luaState, this);
    o.push(luaState);
}